package com.cn.jinl.utils;

import com.cn.jinl.domain.OnDutyProduct;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class BubbleSort {
    public static void sortProduct (List<OnDutyProduct> productList) {
        if(CollectionUtils.isEmpty(productList)){
            return;
        }
        for (int i = 0; i < productList.size() - 1; i++) {
            boolean swapped = false;

            for (int j = 0; j < productList.size() - 1 - i; j++) {
                OnDutyProduct j1 = productList.get(j);
                OnDutyProduct j2 = productList.get(j+1);
                String itemType1 = j1.getItemTypeId();
                String itemType2 = j2.getItemTypeId();
                if(itemType1.indexOf("单") > -1){
                    continue;
                }else if(itemType1.indexOf("多") > -1 && itemType2.indexOf("单") > -1){
                    swap(productList, j, j + 1);
                    swapped = true;
                }else if(itemType1.indexOf("伸") > -1 && (itemType2.indexOf("单") > -1||itemType2.indexOf("多") > -1)){
                    swap(productList, j, j + 1);
                    swapped = true;
                }else if(itemType1.indexOf("U") > -1 ){
                    swap(productList, j, j + 1);
                    swapped = true;
                }
            }

            if (!swapped) {
                break;
            }
        }
    }

    private static void swap(List<OnDutyProduct> productList, int index1, int index2) {
        OnDutyProduct temp = productList.get(index1);
        productList.set(index1, productList.get(index2));
        productList.set(index2, temp);
    }

}
